<?php
/**
 * 推荐商品的管理
 * Author: lyun
 * Date: 2018/4/12
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\App;

use App\Http\Requests\App\RecommendProductV2IndexRequest;
use App\Models\Panx\Product;
use App\Models\Panx\ProductTag;
use App\Models\Store\Store;
use App\Services\Repositories\Product\ProductRepository;
use App\Services\Repositories\Product\RecommendProductV2Repository;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Services\Utils\Guard;
use App\Services\Utils\PanxCollection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use App\Http\Controllers\Controller;

class RecommendProductV2Controller extends Controller
{
    use ApiResponser,
        ApiChecker;

    const ROUTES = [
        'index'                      => 'app.recommend-product-v2.index',
        'index-group-by-category-id' => 'app.recommend-product-v2.index.group.by.category.id',
    ];
    
    /**
     * @since v2.3.6 - 有旧版 App 在使用此接口
     *
     * 根据肤质检测报告，推荐商品
     * @test
     *
     * @param RecommendProductV2IndexRequest $request
     * @param RecommendProductV2Repository $recommendProdRepo
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(RecommendProductV2IndexRequest $request,
                          RecommendProductV2Repository $recommendProdRepo)
    {
        $sex = (int) $request->sex;
        $skin_detector_id = (int) $request->skin_detector_id;
        $type_skin_record_id = (int) $request->type_skin_record_id;
        
        $tag_ids = $recommendProdRepo->getTagIds($skin_detector_id, $type_skin_record_id);
        
        $body_position_id = $recommendProdRepo->getBodyPositionId($skin_detector_id, $type_skin_record_id);
        
        /** @var Store $store */
        $store = Guard::deviceAccount()->store;
    
        $category_id = $recommendProdRepo->getRecommendCategoryId($skin_detector_id, $type_skin_record_id);
    
        $recommend_products = $recommendProdRepo->recommendStoreProducts($store, $tag_ids, $sex, $body_position_id, $category_id);
        
        $recommend_products = $this->assembleProducts($recommend_products);
        
        return $this->response(compact('recommend_products'));
    }
    /**
     * 根据肤质检测报告，推荐商品 (按照category_id分组)
     * @since v2_3_9
     * @test
     *
     * @param RecommendProductV2IndexRequest $request
     * @param RecommendProductV2Repository $recommendProdRepo
     * @return array|\Illuminate\Http\Response
     * @throws \App\Exceptions\ApiAbstractException
     */
    public function indexGroupByCategoryId(RecommendProductV2IndexRequest $request,
                                           RecommendProductV2Repository $recommendProdRepo)
    {
        $sex = (int) $request->sex;
        $skin_detector_id = (int) $request->skin_detector_id;
        $type_skin_record_id = (int) $request->type_skin_record_id;

        $tag_ids = $recommendProdRepo->getTagIds($skin_detector_id, $type_skin_record_id);

        $body_position_id = $recommendProdRepo->getBodyPositionId($skin_detector_id, $type_skin_record_id);

        $category_id = $recommendProdRepo->getRecommendCategoryId($skin_detector_id, $type_skin_record_id);

        /** @var Store $store */
        $store = Guard::deviceAccount()->store;

        $recommend_products = $recommendProdRepo->recommendStoreProducts($store, $tag_ids, $sex, $body_position_id, $category_id);

        $recommend_products = $this->assembleProducts($recommend_products);

        // 根据category_id分组商品
        $group_recommend_products = $recommendProdRepo->transformRecommendProducts($recommend_products);

        return $this->response(compact('group_recommend_products'));
    }

    /**
     * @param Collection $recommend_products
     * @return Collection
     */
    private function assembleProducts(Collection $recommend_products)
    {
        (new PanxCollection($recommend_products))
            ->eachAppend(['tag_names', 'thumb_url', 'link_product', 'brand_name'])
            ->makeHidden(['thumbImage', 'productTags', 'brand']);

        return $recommend_products;
    }
}
